package com.fengniu.ejb.dao.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;
import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.dao.UserDaoLocal;
import com.fengniu.ejb.dao.UserDaoRemote;
import com.fengniu.ejb.dao.util.DaoUtil;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.TagLog;
import com.fengniu.ejb.entity.User;
import com.fengniu.ejb.entity.User2Category;
import com.fengniu.ejb.entity.User2Content;
import com.fengniu.ejb.entity.User2Site;
import com.fengniu.ejb.entity.User2User;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.UserDaoLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.UserDaoRemoteJndiName)
public class UserDaoImpl implements UserDaoLocal, UserDaoRemote {
	static Logger logger = Logger.getLogger(SiteDaoImpl.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -7321946462065370049L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public void deleteUser2CategoryBySysId(long user2CategorySysId) {
		User2Category u2g = em.find(User2Category.class, user2CategorySysId);
		if (u2g != null)
			em.remove(u2g);
	}

	public User2Category findUser2CategoryBy(String emailAddr, Integer[] relationIds, String categoryId) {
		// TODO Auto-generated method stub
		return null;
	}

	public User2Content findUser2ContentBy(long user2ContentSysId) {
		return em.find(User2Content.class, user2ContentSysId);
	}

	public User2Content findUser2ContentBy(String emailAddr, Integer relationId, String contentId) {
		User2Content result = null;
		Integer[] relationIds = { relationId };
		List<User2Content> u2cs = this.getListUser2ContentBy(emailAddr, relationIds, contentId, null, null, null, 0, 1,
				null);
		if (u2cs != null && u2cs.size() > 0) {
			result = u2cs.get(0);
		}
		return result;
	}

	public User2Site findUser2SiteBy(String userId, Integer relationId, String siteId) {
		User2Site result = null;

		Integer[] relationIds = { relationId };

		List<User2Site> u2ss = this.getListUser2SiteBy(userId, relationIds, siteId, null, null, 0, 1, null);
		if (u2ss != null && u2ss.size() > 0)
			result = u2ss.get(0);

		return result;
	}

	public User2Site findUser2SiteBy(long sysId) {
		return em.find(User2Site.class, sysId);
	}

	public User2User findUser2UserBy(long user2UserSysId) {
		// TODO Auto-generated method stub
		return null;
	}

	public User2User findUser2UserBy(String emailAddr, Integer relationId, Integer statusId, String emailAddrTo) {
		// TODO Auto-generated method stub
		return null;
	}

	public User2User findUser2UserBy(long userSysId, Integer relationId, Integer statusId, long userSysId2) {
		User2User result = null;
		Integer[] relationIds = { relationId };
		Integer[] statusIds = { statusId };
		List<User2User> u2uList = this.getListUser2UserBy(userSysId, relationIds, statusIds, userSysId2, 0, 1, null);
		if (u2uList != null && u2uList.size() > 0) {
			result = u2uList.get(0);
		}
		return result;
	}

	public User findUserBySysId(long userSysId) {
		return em.find(User.class, userSysId);
	}

	public User findUserByUserId(String emailAddr) {
		List users = this.findByProperty("userId", emailAddr, 1);
		if (users != null && users.size() > 0)
			return (User) users.get(0);
		return null;
	}

	public long getCountFriendUser2CategoryBy(long loginUserSysId, Integer[] relationIds, String[] statusIds,
			long categorySysId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCountFriendUsers2ContentBy(long loginUserSysId, Integer[] relationIds, long contentSysId,
			String[] statusIds, Integer relationId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCountUser2ContentBy(long userSysId, Integer[] relationIds, long contentSysId, Integer[] statusIds,
			Integer[] contentTypeIds, String[] contentCategoryIds) {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCountUser2UserBy(long userSysId, Integer[] relationIds, Integer[] statusIds, long user2SysId) {
		Long result = new Long(0);
		Query q = this.getQueryUser2UserBy(userSysId, relationIds, user2SysId, statusIds, null, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	public List<User2Category> getListFriendUser2CategoryBy(long loginUserSysId, Integer[] relationIds,
			String[] statusIds, long categorySysId, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<User2Content> getListFriendUsers2ContentBy(long loginUserSysId, Integer[] relationIds,
			long contentSysId, String[] statusIds, Integer relationId, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<User2User> getListUser2UserBy(long userSysId, Integer[] relationIds, Integer[] statusIds,
			long user2SysId, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryUser2UserBy(userSysId, relationIds, user2SysId, statusIds, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public User saveUser(User user) {
		return em.merge(user);
	}

	public User2Category saveUser2Category(User2Category user2Category) {

		return em.merge(user2Category);
	}

	public User2Content saveUser2Content(User2Content user2Content) {
		return em.merge(user2Content);
	}

	public User2Site saveUser2Site(User2Site user2Site) {
		return em.merge(user2Site);
	}

	public User2User saveUser2User(User2User user2User) {
		return em.merge(user2User);
	}

	public long getCountFriendUser2SiteBy(long userSysId, Integer[] relationIds, String[] statusIds, String siteTypeId,
			long siteSysId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<User2Site> getListFriendUser2SiteBy(long userSysId, Integer[] relationIds, String[] statusIds,
			String siteTypeId, long siteSysId, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getCountUser2SiteBy(long userSysId, Integer[] relationIds, long siteSysId, Integer[] statusIds,
			Integer[] siteTypeIds) {
		Long result = new Long(0);
		Query q = this.getQueryUser2SiteBy(userSysId, relationIds, siteSysId, statusIds, siteTypeIds, null, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	public List<User2Site> getListUser2SiteBy(long userSysId, Integer[] relationIds, long siteSysId,
			Integer[] statusIds, Integer[] siteTypeIds, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryUser2SiteBy(userSysId, relationIds, siteSysId, statusIds, siteTypeIds, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public long getCountUser2CategoryBy(long userSysId, Integer[] relationIds, long categorySysId, String[] statusIds,
			String[] userTypeIds) {
		Long result = new Long(0);
		Query q = this.getQueryUser2CategoryBy(userSysId, relationIds, categorySysId, statusIds, userTypeIds, -1, -1,
				null, false);

		result = (Long) q.getSingleResult();

		return result;
	}

	public List<User2Category> getListUser2CategoryBy(long userSysId, Integer[] relationIds, String[] statusIds,
			long categorySysId, String[] userTypeIds, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public void deleteUserByUserId(String userId) {
		User u = findUserByUserId(userId);
		if (u != null)
			em.remove(u);
	}

	public void deleteUserBySysId(long sysId) {
		// TODO Auto-generated method stub

	}

	private Query getQueryUser2ContentBy(String userId, Integer[] relationIds, String contentId, Integer[] statusIds,
			Integer[] contentTypeIds, String[] contentCategoryIds, String orderBy, boolean isGetList) {

		String ent = isGetList ? "u2c" : "count(u2c)";

		String eql = "select " + ent + " from  User2Content u2c  ";
		String whereand = "";
		if (!DaoUtil.isNull(contentCategoryIds)) {
			eql += " Content2Category c2g where u2c.content = c2g.content";
			whereand = " and ";
		}
		if (userId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.user.userId=:userId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.relationId IN (:relationIds) ";
		}
		if (contentId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.content.contentId=:contentId ";
		}
		if (!DaoUtil.isNull(statusIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.status IN (:statusIds) ";
		}
		if (!DaoUtil.isNull(contentTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.content.typeId IN (:contentTypeIds) ";
		}
		if (!DaoUtil.isNull(contentCategoryIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2g.category.categoryId IN (:contentCategoryIds) ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (userId != null) {
			q.setParameter("userId", userId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (contentId != null) {
			q.setParameter("contentId", contentId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			q.setParameter("statusIds", statusIds);
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		if (!DaoUtil.isNull(contentCategoryIds)) {
			List rts = DaoUtil.getListFromArray(contentCategoryIds);
			q.setParameter("contentCategoryIds", rts);
		}
		return q;
	}

	private Query getQueryUser2ContentBy(long userSysId, Integer[] relationIds, long contentSysId, Integer[] statusIds,
			Integer[] contentTypeIds, String[] contentCategoryIds, String orderBy, boolean isGetList) {

		String ent = isGetList ? "u2c" : "count(u2c)";

		String eql = "select " + ent + " from  User2Content u2c  ";
		String whereand = "";
		if (!DaoUtil.isNull(contentCategoryIds)) {
			eql += " Content2Category c2g where u2c.content = c2g.content";
			whereand = " and ";
		}
		if (userSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.user.sysId=:userSysId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.relationId IN (:relationIds) ";
		}
		if (contentSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.content.sysId=:contentSysId ";
		}
		if (!DaoUtil.isNull(statusIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.status IN (:statusIds) ";
		}
		if (!DaoUtil.isNull(contentTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c.content.typeId IN (:contentTypeIds) ";
		}
		if (!DaoUtil.isNull(contentCategoryIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2g.category.categoryId IN (:contentCategoryIds) ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (userSysId > -1) {
			q.setParameter("userSysId", userSysId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (contentSysId > -1) {
			q.setParameter("contentSysId", contentSysId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}
		if (DaoUtil.isNull(contentTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(contentTypeIds);
			q.setParameter("contentTypeIds", rts);
		}

		if (!DaoUtil.isNull(contentCategoryIds)) {
			List rts = DaoUtil.getListFromArray(contentCategoryIds);
			q.setParameter("contentCategoryIds", rts);
		}
		return q;
	}

	private Query getQueryUser2SiteBy(long userSysId, Integer[] relationIds, long siteSysId, Integer[] statusIds,
			Integer[] siteTypeIds, String orderBy, boolean isGetList) {

		String ent = isGetList ? "u2s" : "count(u2s)";

		String eql = "select " + ent + " from  User2Site u2s  ";
		String whereand = "";

		if (userSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.user.sysId=:userSysId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.relationId IN (:relationIds) ";
		}
		if (siteSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.site.sysId=:siteSysId ";
		}
		if (!DaoUtil.isNull(statusIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.status IN (:statusIds) ";
		}
		if (!DaoUtil.isNull(siteTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.site.typeId IN (:siteTypeIds) ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (userSysId > -1) {
			q.setParameter("userSysId", userSysId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (siteSysId > -1) {
			q.setParameter("siteSysId", siteSysId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}
		if (DaoUtil.isNull(siteTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(siteTypeIds);
			q.setParameter("siteTypeIds", rts);
		}

		return q;
	}

	private Query getQueryUser2UserBy(long userSysId, Integer[] relationIds, long userSysId2, Integer[] statusIds,
			String orderBy, boolean isGetList) {

		String ent = isGetList ? "u2u" : "count(u2u)";

		String eql = "select " + ent + " from  User2User u2u  ";
		String whereand = "";

		if (userSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2u.user.sysId=:userSysId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2u.relationId IN (:relationIds) ";
		}
		if (userSysId2 > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2u.user2.sysId=:userSysId2 ";
		}
		if (!DaoUtil.isNull(statusIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2u.status IN (:statusIds) ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (userSysId > -1) {
			q.setParameter("userSysId", userSysId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (userSysId2 > -1) {
			q.setParameter("userSysId2", userSysId2);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			List rts = DaoUtil.getListFromArray(statusIds);
			q.setParameter("statusIds", rts);
		}

		return q;
	}

	private Query getQueryUser2SiteBy(String userId, Integer[] relationIds, String siteId, String[] statusIds,
			String[] siteTypeIds, String orderBy, boolean isGetList) {

		String ent = isGetList ? "u2s" : "count(u2s)";

		String eql = "select " + ent + " from  User2Site u2s  ";
		String whereand = "";

		if (userId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.user.userId=:userId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.relationId IN (:relationIds) ";
		}
		if (siteId != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.site.siteId=:siteId ";
		}
		if (!DaoUtil.isNull(statusIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.status IN (:statusIds) ";
		}
		if (!DaoUtil.isNull(siteTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2s.site.typeId IN (:siteTypeIds) ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (userId != null) {
			q.setParameter("userId", userId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (siteId != null) {
			q.setParameter("siteId", siteId);
		}
		if (DaoUtil.isNull(statusIds) == false) {
			q.setParameter("statusIds", statusIds);
		}
		if (DaoUtil.isNull(siteTypeIds) == false) {
			List rts = DaoUtil.getListFromArray(siteTypeIds);
			q.setParameter("siteTypeIds", rts);
		}

		return q;
	}

	private Query getQueryUserBy(Date afterLastUpdatedDate, String orderBy, boolean isGetList) {

		String ent = isGetList ? "u" : "count(u)";

		String eql = "select " + ent + " from  User  u  ";
		String whereand = "";

		if (afterLastUpdatedDate != null) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u.lastUpdatedDate>=:afterLastUpdatedDate ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (afterLastUpdatedDate != null) {
			q.setParameter("afterLastUpdatedDate", afterLastUpdatedDate);
		}

		return q;
	}

	private Query getQueryUser2CategoryBy(long userSysId, Integer[] relationIds, long categorySysId,
			String[] statusIds, String[] userTypeIds, int pgno, int pgsize, String orderBy, boolean isGetList) {

		String ent = isGetList ? "c2g" : "count(c2g)";

		String eql = "select " + ent + " from  User2Category c2g";
		String whereand = "";
		if (userSysId > 0) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.user.sysId=:userSysId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.relationId IN(:relationIds) ";
		}
		if (categorySysId > 0) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.category.sysId=:categorySysId ";
		}

		if (!DaoUtil.isNull(statusIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.status IN (:statusIds) ";
		}

		if (!DaoUtil.isNull(userTypeIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  c2g.user.typeId IN (:userTypeIds) ";
		}
		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (userSysId > 0) {
			q.setParameter("userSysId", userSysId);
		}
		if (!DaoUtil.isNull(relationIds)) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (categorySysId > 0) {
			q.setParameter("categorySysId", categorySysId);
		}
		if (!DaoUtil.isNull(statusIds)) {
			q.setParameter("statusIds", statusIds);
		}
		if (!DaoUtil.isNull(userTypeIds)) {
			List rts = DaoUtil.getListFromArray(userTypeIds);
			q.setParameter("userTypeIds", rts);
		}

		return q;
	}

	public List<User2Category> getListUser2CategoryBy(long userSysId, Integer[] relationIds, long categorySysId,
			String[] statusIds, String[] userTypeIds, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryUser2CategoryBy(userSysId, relationIds, categorySysId, statusIds, userTypeIds, pgno,
				pgsize, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public User2Content findUser2ContentBy(long userSysId, Integer relationId, long contentSysId) {
		User2Content result = null;
		Integer[] relationIds = { relationId };
		List<User2Content> u2cs = this.getListUser2ContentBy(userSysId, relationIds, contentSysId, null, null, null, 0,
				1, null);
		if (u2cs != null && u2cs.size() > 0) {
			result = u2cs.get(0);
		}
		return result;
	}

	// -----------------------------------------------------
	@SuppressWarnings("unchecked")
	private List<User> findByProperty(String propertyName, Object value, int size) {
		String queryString = "select model from User model where model." + propertyName + "= :propertyValue";
		return em.createQuery(queryString).setParameter("propertyValue", value).setFirstResult(0).setMaxResults(size)
				.getResultList();
	}

	public User2Category findUser2CategoryBySysId(long user2CategorySysId) {
		return em.find(User2Category.class, user2CategorySysId);
	}

	public void deleteUser2ContentBySysId(long user2ContentSysId) {
		User2Content u2c = em.find(User2Content.class, user2ContentSysId);
		if (u2c != null)
			em.remove(u2c);
	}

	public void deleteUser2SiteBySysId(long user2SiteSysId) {
		// TODO Auto-generated method stub

	}

	public long getCountUser2CategoryBy(String userId, Integer[] relationIds, String categoryId, String[] statusIds,
			String[] userTypeIds, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCountUser2ContentBy(String userId, Integer[] relationIds, String contentId, Integer[] statusIds,
			Integer[] contentTypeIds, String[] contentCategoryIds) {
		Long result = new Long(0);
		Query q = this.getQueryUser2ContentBy(userId, relationIds, contentId, statusIds, contentTypeIds,
				contentCategoryIds, null, false);

		result = (Long) q.getSingleResult();
		return result;
	}

	public List<User2Content> getListUser2ContentBy(String userId, Integer[] relationIds, String contentId,
			Integer[] statusIds, Integer[] contentTypeIds, String[] contentCategoryIds, int pgno, int pgsize,
			String orderBy) {
		Query q = this.getQueryUser2ContentBy(userId, relationIds, contentId, statusIds, contentTypeIds,
				contentCategoryIds, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public List<User2Content> getListUser2ContentBy(long userSysId, Integer[] relationIds, long contentSysId,
			Integer[] statusIds, Integer[] contentTypeIds, String[] contentCategoryIds, int pgno, int pgsize,
			String orderBy) {
		Query q = this.getQueryUser2ContentBy(userSysId, relationIds, contentSysId, statusIds, contentTypeIds,
				contentCategoryIds, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public long getCountUser2SiteBy(String userId, Integer[] relationIds, String siteId, String[] siteTypeIds,
			String[] statusIds) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<User2Category> getListUser2CategoryBy(String userId, Integer[] relationIds, String categoryId,
			String[] statusIds, String[] userTypeIds, int pgno, int pgsize, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<User2Site> getListUser2SiteBy(String userId, Integer[] relationIds, String siteId,
			String[] siteTypeIds, String[] statusIds, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryUser2SiteBy(userId, relationIds, siteId, statusIds, siteTypeIds, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	public void deleteUser2Contents(long userSysId, Integer relationId, long[] contentSysIds) {
		String eql = "delete from User2Content u2c where u2c.user.sysId=:userSysId and u2c.relationId=:id and u2c.content.sysId IN (:contentSysIds)";

		Query q = em.createQuery(eql);

		q.setParameter("userSysId", userSysId);
		q.setParameter("id", relationId);
		q.setParameter("contentSysIds", DaoUtil.getListFromArray(contentSysIds));

		q.executeUpdate();

	}

	public long getCountUserBy(Date afterLastUpdatedDate) {
		Long result = new Long(0);
		Query q = this.getQueryUserBy(afterLastUpdatedDate, null, false);

		result = (Long) q.getSingleResult();
		return result;
	}

	public List<User> getListUserBy(Date afterLastUpdatedDate, int pgno, int pgsize, String orderBy) {
		Query q = this.getQueryUserBy(afterLastUpdatedDate, orderBy, true);
		List result = DaoUtil.getResultList(q, pgno, pgsize);
		return result;
	}

	// public Site2User_Content findUser2Content2Site(long user2ContentSysId,
	// Integer relationId, long siteSysId) {
	// String eql = "select u2c2s from User2Content2Site u2c2s where
	// u2c2s.user2Content.sysId=:user2ContentSysId and
	// u2c2s.relationId=:relationId and u2c2s.site.sysId=:siteSysId";
	// Query q = em.createQuery(eql);
	//
	// q.setParameter("siteSysId", siteSysId);
	// q.setParameter("relationId", relationId);
	// q.setParameter("user2ContentSysId", user2ContentSysId);
	// List<Site2User_Content> result =
	// q.setFirstResult(0).setMaxResults(1).getResultList();
	// if (result != null && result.size() > 0) {
	// return result.get(0);
	// }
	// return null;
	// }
	//
	// public void saveUser2Content2Site(Site2User_Content user2Content2Site) {
	// em.persist(user2Content2Site);
	// }

	public void deleteUser2Content2Sites(long user2Content2SiteSysId, Integer relationId, long[] siteSysIds) {
		String eql = "DELETE FROM User2Content2Site u2c2s where u2c2s.sysId=:sysId and u2c2s.relationId=:relationId";
		if (siteSysIds != null && siteSysIds.length > 0) {
			eql += " and u2c2s.site.sysId IN (:siteSysIds)";
		}
		Query q = em.createQuery(eql);
		q.setParameter("sysId", user2Content2SiteSysId);
		q.setParameter("relationId", relationId);
		if (siteSysIds != null && siteSysIds.length > 0) {
			q.setParameter("siteSysIds", DaoUtil.getListFromArray(siteSysIds));
		}
		q.executeUpdate();
	}

	// public List<Site2User_Content> getListUser2Content2SiteBy(long
	// user2ContentSysId, Integer[] relationIds,
	// long siteSysId, int pgno, int pgsize, String orderBy) {
	// Query q = this.getQueryUser2Content2SiteBy(user2ContentSysId,
	// relationIds, siteSysId, orderBy, true);
	// List result = DaoUtil.getResultList(q, pgno, pgsize);
	// return result;
	// }

	public long getCountUser2Content2SiteBy(long user2ContentSysId, Integer[] relationIds, long siteSysId) {
		Long result = new Long(0);
		Query q = this.getQueryUser2Content2SiteBy(user2ContentSysId, relationIds, siteSysId, null, false);

		result = (Long) q.getSingleResult();
		return result;
	}

	private Query getQueryUser2Content2SiteBy(long user2ContentSysId, Integer[] relationIds, long siteSysId,
			String orderBy, boolean isGetList) {

		String ent = isGetList ? "u2c2s" : "count(u2c2s)";

		String eql = "select " + ent + " from  User2Content2Site u2c2s  ";
		String whereand = "";

		if (user2ContentSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c2s.user2Content.sysId=:user2ContentSysId ";
		}
		if (!DaoUtil.isNull(relationIds)) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c2s.relationId IN (:relationIds) ";
		}
		if (siteSysId > -1) {
			whereand = DaoUtil.getWhereOrAndStr(whereand);
			eql += whereand + "  u2c2s.site.sysId=:siteSysId ";
		}

		logger.info("eql=[" + eql + "]");
		Query q = em.createQuery(eql);
		if (user2ContentSysId > -1) {
			q.setParameter("user2ContentSysId", user2ContentSysId);
		}
		if (DaoUtil.isNull(relationIds) == false) {
			List rts = DaoUtil.getListFromArray(relationIds);
			q.setParameter("relationIds", rts);
		}
		if (siteSysId > -1) {
			q.setParameter("siteSysId", siteSysId);
		}

		return q;
	}

	public User2Site findUser2SiteBy(long userSysId, Integer relationId, long siteSysId) {
		User2Site result = null;
		Integer[] relationIds = { relationId };
		List<User2Site> u2ss = this.getListUser2SiteBy(userSysId, relationIds, siteSysId, null, null, 0, 1, null);
		if (u2ss != null && u2ss.size() > 0)
			result = u2ss.get(0);
		return result;
	}

	public User findUserByArbitraryId(String arbitraryUserId) {
		if (arbitraryUserId != null && arbitraryUserId.trim() != "") {
			String id = arbitraryUserId.trim();
			Query q = em.createNamedQuery("User.findUserByArbitraryId").setParameter("userId", id).setParameter("tel",
					id).setParameter("mobile", id).setParameter("qqId", id);
			List<User> userList = q.setFirstResult(0).setMaxResults(1).getResultList();
			if (userList != null && userList.size() > 0) {
				return userList.get(0);
			}
		}
		return null;
	}

	public TagLog saveTagLog(TagLog tagLog) {
		return em.merge(tagLog);
	}
}
